<?php
/**
 * Created by PhpStorm.
 * User: ZhangWei
 * Date: 2018/3/12
 * Time: 10:28
 */

namespace App\Logic;


use App\Helpers\UserBuyStatus;
use App\Models\Doc;
use App\Models\UserBuy;
use App\User;

class UserBuyLogic
{
    const free_user = [];

    /**
     * @param $doc
     * @param $user
     * @return UserBuyStatus
     */
    public static function judgeDoc($doc, $user)
    {
        if ($user) {
            $is_sub = (boolean)$doc->isSubscribedBy($user);
        } else {
            $is_sub = false;
            if ($doc->doc_type == 0) {
                return UserBuyStatus::make(true, false, false, $is_sub, false, 'doc', 'subscribe');
            }else{
                return UserBuyStatus::make(false, false, false, $is_sub, false, 'doc', 'subscribe');
            }
        }

        if ($doc->user_id == $user->id || in_array($user->id, self::free_user)) {
            return UserBuyStatus::make(true, false, true, $is_sub, false, 'doc', 'subscribe');
        }
        if ($doc->doc_type == 0) {//免费 返回是否订阅
            return UserBuyStatus::make(true, false, false, $is_sub, false, 'doc', 'subscribe');
        } elseif ($doc->doc_type == 1) {//付费专栏 返回是否购买
            $ckeckBuy = UserBuy::checkBuy(Doc::class, $doc->id, $user->id);
            if (empty($ckeckBuy)) {
                return UserBuyStatus::make(false, false, false, $is_sub, false, 'doc', 'buy');
            } else {
                return UserBuyStatus::make(true, true, false, $is_sub, false, 'doc', 'buy');
            }

        }
    }

    /**
     * 判断专栏权限
     * @param $channel 专栏
     * @param $user 用户
     * @return UserBuyStatus
     */
    public static function judgeChannel($channel, $user)
    {
        if ($channel->user_id == $user->id || in_array($user->id, self::free_user)) {
            return UserBuyStatus::make(true, false, true, true, false, 'channel', 'subscribe');
        }
        if ($channel->channel_type == 0) {//免费专栏 返回是否订阅
            if ($channel->isSubscribedBy($user)) {
                return UserBuyStatus::make(true, false, false, true, false, 'channel', 'subscribe');
            } else {
                return UserBuyStatus::make(false, false, false, false, false, 'channel', 'subscribe');
            }
        } elseif ($channel->channel_type == 1) {//付费专栏 返回是否购买
            $ckeckBuy = UserBuy::checkBuy('channel', $channel->id, $user->id);
            if (empty($ckeckBuy)) {
                return UserBuyStatus::make(false, false, false, false, false, 'channel', 'buy');
            } else {
                return UserBuyStatus::make(true, true, false, false, false, 'channel', 'buy');
            }

        }

    }

    /**
     * 判断课程权限
     * @param $lecture 课程
     * @param $user 用户
     * @return UserBuyStatus static
     */
    public static function judgeLecture($lecture, $user)
    {
        if ($lecture->user_id == $user->id || in_array($user->id, self::free_user)) {
            return UserBuyStatus::make(true, false, true, true, false, 'lecture', 'subscribe');
        }
        if ($lecture->lecture_type == 0) {//免费课程 返回通过
            return UserBuyStatus::make(true, false, false, true, false, 'lecture', 'subscribe');
        } elseif ($lecture->lecture_type == 1) {//付费课程
            $channle = $lecture->channel;
            if ($channle) {//判断所属专栏是否已经购买
                $judgeChannel = self::judgeChannel($channle, $user);
                if ($judgeChannel->getIsBuy()) {
                    return UserBuyStatus::make(true, false, true, true, false, 'lecture', 'buy');
                }
            }
            //判断课程是否已经购买
            $ckeckBuy = UserBuy::checkBuy('lecture', $lecture->id, $user->id);
            if (empty($ckeckBuy)) {
                return UserBuyStatus::make(false, false, false, false, false, 'lecture', 'buy');
            } else {
                return UserBuyStatus::make(true, true, false, false, false, 'lecture', 'buy');
            }

        }

    }

    /**
     * @param $item
     * @param $user
     * @param $invite_id
     * @return bool|\Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model|null|static|static[]
     */
    public static function getTgUser($item, $user, $invite_id)
    {
        if ($invite_id > 0 /*&& $invite_id !== $user->id && $invite_id !== $item->user_id*/) {
            $tg_user = User::query()->find($invite_id);
        } else {
            $parent_id = $user->parent_id;
            if ($parent_id > 0) {
                $tg_user = User::query()->find($parent_id);
            } else {
                $tg_user = false;
            }
        }
        return $tg_user;
    }

}